Skip to content

Handle websocket binary response from ComfyUI server#2

Open
guinoise wants to merge 2 commits intoandreyryabtsev:mainfrom
guinoise:feature-websocketbinary
Open

Handle websocket binary response from ComfyUI server#2
guinoise wants to merge 2 commits intoandreyryabtsev:mainfrom
guinoise:feature-websocketbinary

Conversation

@guinoise
Copy link
Copy Markdown

When I tried the tool installed via pip, I got issues. The main cause of this is that the ComfyUI server return sampler images and that was not handled in the library. I fixed that and made some minor changes.

If you can accept the request and push a new version to pypi it will be greatly appreciated.

What changed :

  • Handle websocket binary response from ComfyUI server
  • Convert print to logger
  • Add option to store output file in directory
  • Add option to store preview images in a directory
  • Change the README accordingly
  • Better handling of Content type error on server response (I found it's cause by my reverse proxy when the backend is down, with the new handling, messages).

Convert print to logger
Add option to store output file in directory
Add option to store preview images in a directory
Change the README accordingly
Copy link
Copy Markdown
Owner

@andreyryabtsev andreyryabtsev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this PR! Just took a brief pass with some comments, but looking forward to trying it locally + merging ASAP :)

"""Called when the prompt completes, with the final output."""
@abc.abstractmethod
async def image_received(self, image: Image.Image):
"""Called when the prompt's queue return a sampler image, a pillow image object is pass to this function"""
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "is pass" -> "is passed" + period at end just for consistency

total = int(message["data"]["max"])
await callbacks.in_progress(current_node, progress, total)
elif msg.type == aiohttp.WSMsgType.BINARY:
image= await receive_image(msg.data)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: space before "="

async def receive_image(image_data) -> Optional[Image.Image]:
'''
Reference : https://github.com/comfyanonymous/ComfyUI.git server.py function send_image
Rebuild an PIL Image from the data received on the websocket. Return None on any errors.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "a PIL image" + perhaps it's ok to skip the try and just let exception bubble up (ComfyAPI already throws exceptions)

raise ValueError("\n" + "\n".join(errors))

prompt_id = response_json['prompt_id']
except aiohttp.client_exceptions.ContentTypeError as e:
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can the except be moved up to only wrap the await resp.json() so any other errors bubble up normally?

@@ -1,13 +1,27 @@
"""End-to-end example for creating an image with SDXL base + refiner"""
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we keep the file comment?

import sys

formatter = logging.Formatter('%(asctime)s [%(levelname)-9s][%(name)-20s] %(message)s')
sh= logging.StreamHandler(stream=sys.stdout)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: space before "="/"+=" throughout the file to follow standard style?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants